<?php

namespace app\controllers;

use app\models\Category;
use Yii;

class CategoryController extends BasisController
{
    public function actionSave()
    {
        $post = Yii::$app->request->post();

        $saveObj = !isset($post['id']) || empty($post['id']) ? new Category() : Category::findOne($post['id']);

        $saveData = [
            'name' => isset($post['name']) ? $post['name'] : '',
            'sort' => isset($post['sort']) ? $post['sort'] : '',
            'desc' => isset($post['desc']) ? $post['desc'] : '',
            'pid' => isset($post['pid']) ? $post['pid'] : '0',
            'project_id' => isset($post['projectId']) ? $post['projectId'] : '',
            'created_uid' => Yii::$app->user->id ? Yii::$app->user->id : '0',
            'created_time' => time(),
            'is_del' => isset($post['is_del']) ? $post['is_del'] : '0'

        ];
        $saveObj->attributes = $saveData;

        if ($saveObj->validate() && $saveObj->save()){
            $saveData['id'] = $saveObj->attributes['id'];
            $this->ajaxReturn(['e' => '0' , 'm' => '恭喜您，操作成功','d' => $saveData]);
        } else {
            $this->ajaxReturn(['e' => '1' , 'm' => '很遗憾，操作失败','d' => $saveObj->getErrors()]);
        }
    }
}
